from django.shortcuts import render

# Create your views here.


# get /areas/
from rest_framework.generics import GenericAPIView, ListAPIView, RetrieveAPIView
from rest_framework.response import Response
from rest_framework.viewsets import ViewSet, GenericViewSet, ReadOnlyModelViewSet
from rest_framework_extensions.cache.mixins import CacheResponseMixin

from areas.models import Areas
from areas.serializers import AreasSerializer, SubsAreasSerializer

class AreasSetView(CacheResponseMixin,ReadOnlyModelViewSet):
    '''
    三级联动
    '''
    def get_serializer_class(self):
        if self.action =='list':
            return AreasSerializer
        else:
            return SubsAreasSerializer
    def get_queryset(self):
        if self.action =='list':
            return Areas.objects.filter(parent=None)
        else:
            return Areas.objects.all()
    # def list(self, request):
    #     areas = self.get_queryset()
    #     serializer = self.get_serializer(areas, many=True)
    #     return Response(serializer.data)
    # def retrieve(self, request, pk):
    #     areas = self.get_object()
    #     serializer = self.get_serializer(areas)
    #     return Response(serializer.data)
# /areas/(?P<pk>\d+)/
# class SubsAreasView(RetrieveAPIView):
#     queryset = Areas.objects.all()
#     serializer_class = SubsAreasSerializer
#     # def get(self, request, pk):
#     #     areas = self.get_object()
#     #     serializer = self.get_serializer(areas)
#     #     return Response(serializer.data)
#
# # /areas/
# class AreasView(ListAPIView):
#     # 第一栏地址
#     serializer_class = AreasSerializer
#     queryset = Areas.objects.filter(parent=None)
#     # def get(self, request):
#     #     areas = self.get_queryset()
#     #     serializer = self.get_serializer(areas, many=True)
#     #     return Response(serializer.data)